<?php
namespace Admin\Controller;
use Lib\Controller\AdminBaseController;

class MenuController extends AdminBaseController{
    
    public function menu_list(){
        $MenuModel = D("Menu");
        $menuList = $MenuModel->select();
//         p(arrayMerge($menuList));
        $this->assign('menuList', arrayMerge($menuList));
//         p($MenuModel->test());
        $this->display();
    }
    
    /**
     * 添加菜单
     */
    public function add_menu() {
        $MenuModel = D("Menu");
        if (IS_POST) {
            $data = array();
            //获取参数
            $type = I('post.type', '');
            //错误信息
            $errorInfo = '';
            switch ($type) {
                case 'group':
                    $data['p_id'] = 0;
                    $data['name'] = I('post.title', ''); //组名
                    if (empty($data['name'])) {
                        $errorInfo = '分组名不能为空！';
                        break;
                    }
                    if ($this->_checkMenuOnlyOne('name', $data['title'])) {
                        $errorInfo = '分组名重复！';
                    }
                    break;
                case 'class':
                    $data['p_id'] = I('post.pid', 0, 'int'); //父id
                    $data['name'] = I('post.title', ''); //类名
                    if ($data['p_id'] <= 0) {
                        $errorInfo = '父id错误！';
                        break;
                    }
                    if (empty($data['name'])) {
                        $errorInfo = '分类名不能为空！';
                        break;
                    }
                    if ($this->_checkMenuOnlyOne('name', $data['name'])) {
                        $errorInfo = '分类名重复！';
                    }
                    break;
                case 'menu':
                    $data['p_id'] = I('post.pid', 0, 'int'); //父id
                    $data['name'] = I('post.title', ''); //名字
                    $data['operation'] = I('post.name','');//方法
                    $data['parameter'] = I('post.parameter', ''); //参数
                    $data['title'] = I('post.title', ''); //方法名
                    if ($data['p_id'] <= 0) {
                        $errorInfo = '父id错误！';
                        break;
                    }
                    if (empty($data['name'])) {
                        $errorInfo = '菜单名不能为空！';
                        break;
                    }
                    if (empty($data['operation'])) {
                        $errorInfo = '方法名不能为空！';
                        break;
                    }
                    if ($this->_checkMenuOnlyOne('name', $data['name'])) {
                        $errorInfo = '菜单名重复！';
                        break;
                    }
                    if ($this->_checkMenuOnlyOne('operation', $data['operation'])) {
                        $errorInfo = '方法名重复！';
                    }
                    break;
                default:
                    $errorInfo = 'type参数错误！';
                    break;
            }
//             p($data);
            if (!empty($errorInfo)) {
                $this->tyAjaxReturn(0, $errorInfo);
            }
            $data['name'] = I('post.title');
            $data['is_show'] = I('post.is_show', 0, 'int');
            $data['status'] = I('post.status', 1, 'int');
            $data['remark'] = I('post.remark', '');
            $data['sort'] = I('post.sort', 0, 'int');
            
            $result = $MenuModel->add($data);
            if ($result) {
                $this->tyAjaxReturn(1, '添加成功', U(MODULE_NAME.'/Menu/menu_list'));
            } else {
                $this->tyAjaxReturn(0, '添加失败');
            }
        } else {
            $pid = I('get.id', '', 'int');
            //类型
            $type = I('get.type', '');
            if ($type == 'class' || $type == 'menu') {
                $this->assign('pid', $pid);
            } else {
                $this->assign('pid', NULL);
            }
            $menuInfo = $MenuModel->find($pid);
            $this->assign('type', $type);
            $this->assign('menuInfo', $menuInfo);
            $this->display('edit_menu');
        }
    }
    
    /**
     * 编辑菜单
     */
    public function edit_menu() {
        if (IS_POST) {
            $MenuModel = D("Menu");
            $data = array();
            $data['id'] = I('post.id', '', 'int');
//             $data['name'] = I('post.name', NULL);
            $data['parameter'] = I('post.parameter', NULL);
            $data['name'] = I('post.title', '');
            $data['is_show'] = I('post.is_show', 1, 'int');
            $data['status'] = I('post.status', 1, 'int');
            $data['remark'] = I('post.remark', '');
            $data['sort'] = I('post.sort', 0, 'int');
//             if ($data['title'] == '') {
//                 $this->tyAjaxReturn(0, '菜单名不能为空！');
//             }
            $result = $MenuModel->save($data);
//             p($MenuModel->getLastSql());
            if ($result !== FALSE) {
                $this->tyAjaxReturn(1, '修改成功！', U(MODULE_NAME.'/Menu/menu_list'));
            } else {
                $this->tyAjaxReturn(0, '修改失败！');
            }
        } else {
            $MenuModel = D("Menu");
            $id = I('get.id', '', 'int');
            $type = I('get.type', '');
            if ($id == '' || $id < 0 || $type == '') {
                $this->error('参数错误！');
            }
            $menuInfo = $MenuModel->where('id = '.$id)->find();
            $this->assign('type', $type);
            $this->assign('menuInfo', $menuInfo);
            $this->display();
        }
    }
    
    /**
     * 验证菜单唯一
     * @param type $field 字段名
     * @param type $data 数据
     * @return boolean
     */
    private function _checkMenuOnlyOne($field, $data) {
        $MenuModel = D("Menu");
        $count = $MenuModel->where(array($field => $data))->count();
//         p($MenuModel->getLastSql());
        if ($count > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
    
    /**
     * 删除菜单
     */
    public function delete_menu(){
        $MenuModel = D("Menu");
        $where['id'] = I('get.id');
        
        $check = $MenuModel->where('p_id = '.$where['id'])->count();
        if($check>0){
            $info['status'] = 0;
            $info['info'] = '该菜单有二级菜单尚未删除';
            $this->ajaxReturn($info);
        }else{
            $delete = $MenuModel->where($where)->delete();
            if($delete){
                $info['status'] = 1;
                $info['info'] = '删除成功';
                $info['url'] = U(MODULE_NAME.'/Menu/menu_list');
                S('menuList', NULL);
                $this->ajaxReturn($info);
            }else{
                $info['status'] = 0;
                $info['info'] = '删除失败';
                $this->ajaxReturn($info);
            }
        }
    }
    
    /**
     * 异步修改菜单状态
     */
    public function change_menu_status() {
        $MenuModel = D("Menu");
        $id = I('get.id', '', 'int');
        if ($id == '') {
            $this->tyAjaxReturn(0, '修改失败');
        }
        //查询之前状态
        $data = $MenuModel->field('status')->find($id);
    
        $status = $data['status'] > 0 ? 0 : 1;
    
        $result = $MenuModel->where(array('id' => $id))->save(array('status' => $status));
    
        if ($result > 0) {
            $this->tyAjaxReturn(1, '修改成功');
        } else {
            $this->tyAjaxReturn(0, '修改失败');
        }
    }
    
    /**
     * 异步修改显示
     */
    public function change_menu_is_show() {
        $MenuModel = D("Menu");
        $id = I('get.id', '', 'int');
        if ($id == '') {
            $this->tyAjaxReturn(0, '修改失败');
        }
        //查询之前状态
        $data = $MenuModel->field('is_show')->find($id);
    
        $isShow = $data['is_show'] > 0 ? 0 : 1;
    
        $result = $MenuModel->where(array('id' => $id))->save(array('is_show' => $isShow));
    
        if ($result > 0) {
            $this->tyAjaxReturn(1, '修改成功');
        } else {
            $this->tyAjaxReturn(0, '修改失败');
        }
    }
}
?>